package com.ruipeng.shengyapo.pay.wx;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ruipeng.shengyapo.pay.wx.http.HttpClientConnectionManager;
import com.ruipeng.shengyapo.utils.XmlUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public class GetWxOrderno
{
  public static DefaultHttpClient httpclient;

  static
  {
    httpclient = new DefaultHttpClient();
    //httpclient = (DefaultHttpClient) HttpClientConnectionManager.getSSLInstance(httpclient);
  }
	public static String getPayNo(String url,String xmlParam){
		//  System.out.println("xml是:"+xmlParam);
		DefaultHttpClient client = new DefaultHttpClient();
		//client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		//HttpPost httpost= HttpClientConnectionManager.getPostMethod(url);
		HttpPost httpost= new HttpPost(url);
				String prepay_id = "";
		try {
			httpost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			HttpResponse response = httpclient.execute(httpost);
			String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
			Map<String, Object> dataMap = new HashMap<String, Object>();
			     System.out.println("json是:"+jsonStr);

			if(jsonStr.indexOf("FAIL")!=-1){
				return prepay_id;
			}
			Map map = XmlUtil.doXMLParse(jsonStr);
			System.out.println("mapInfo = " + map.toString());
			String return_code  = (String) map.get("return_code");
			prepay_id  = (String) map.get("prepay_id");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return prepay_id;
	}

  public static InputStream String2Inputstream(String str) {
		return new ByteArrayInputStream(str.getBytes());
	}


}